package com.wsl.tibet_sprcifcation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wsl.pojo.Tb_item_cat;
import com.wsl.tibet_sprcifcation.dao.Tb_item_catDao;
import com.wsl.service.Tb_item_catService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class Tb_item_catServiceimpl implements Tb_item_catService {

    @Autowired
    private Tb_item_catDao tb_item_catDao;

    //商品分类点开就要看到一级菜单
    //一级菜单默认为0
    @Override
    public List<Tb_item_cat> findallitemcat(Integer id) {
        QueryWrapper q1 = new QueryWrapper();
        q1.eq("parent_id", id);
        return tb_item_catDao.selectList(q1);
    }

    //修改
    @Override
    public void updatetem(Tb_item_cat tb_item_cat) {
        tb_item_catDao.updateById(tb_item_cat);
    }

    //删除三级菜单
    @Override
    public void deletetem(Long id) {
//        查询一级
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", id);
//        返回结果
        List<Tb_item_cat> tb_item_cat = tb_item_catDao.selectList(queryWrapper);
        if (tb_item_cat.size()!=0){
            //        遍历一级结果查询二级
            for (Tb_item_cat item : tb_item_cat) {
                QueryWrapper q2 = new QueryWrapper();
                q2.eq("parent_id", item.getId());
//        返回二级结果
                List<Tb_item_cat> tb_item_cat2 = tb_item_catDao.selectList(q2);
                if (tb_item_cat2.size()!=0) {
                    //        遍历二级结果查询三级
                    for (Tb_item_cat item2 : tb_item_cat2) {
                      tb_item_catDao.deleteById(item2.getId());
                    }
                } else {
                    tb_item_catDao.deleteById(item.getId());
                }
                tb_item_catDao.deleteById(item.getId());
            }
            tb_item_catDao.deleteById(id);
        } else {
            tb_item_catDao.deleteById(id);
        }
    }
}


